﻿$(document).ready(function() {
    $("#tree").bind("loaded.jstree", function(event, data) {
        $("#tree").jstree("open_all");
    });

    $("#tree").bind("refresh.jstree", function(event, data) {
        $("#tree").jstree("open_all");
    });

    $(function() {
        $("#tree").jstree({
            plugins: ["themes", "html_data", "ui", "checkbox"],
            ui: { theme_name: "classic" },
            checkbox: { two_state: "true" },
            rules: {
                metadata: "mdata",
                use_inline: true
            }
        });

    });

    $("#applyFilters").live("click", function() {
        var checked_ids = [];
        $("#tree").jstree("get_checked", null, true).each
            (function() {
                checked_ids.push(this.id);
            });
        $.ajax({
            type: 'POST',
            url: '/Poll/ApplyQuestionReportTextualFilters',
            traditional: true,
            data: ({ pollID: $("#pollID").val(), entityIDs: checked_ids }),
            cache: false,
            async: true,
            success: function(html) {
                $('#reportTextual').html(html);
            }
        });
        return false;
    });

    $("#generatePDF").live("click", function() {
        var checked_ids = [];
        $("#tree").jstree("get_checked", null, true).each
                (function() {
                    checked_ids.push(this.id);
                });
        $.ajax({
            type: 'POST',
            url: '/Poll/ApplyQuestionReportTextualFilters',
            traditional: true,
            data: ({ pollID: $("#pollID").val(), entityIDs: checked_ids }),
            cache: false,
            async: false,
            success: function(html) {
                $('#reportTextual').html(html);
            }
        });
        return true;
    });
});

